package 力扣日常刷题.木23一月.第01天301;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 帅小伙
 * @date 2022/3/1
 * @description
 */
public class Demo11合并两个有序数组 {


    public static void main(String[] args) {
        int[] a = new int[]{1,2,3,4,5,0,0,0,0};
        int[] b = new int[]{3,4,5,6};
        a = merge(a,5,b,4);



        for (int i : a) {
            System.out.print(i + " ");
        }

    }

    public static int[] merge(int[] a,int n, int[] b,int m) {
        int i = n - 1, j = m - 1;
        int count = n+m - 1;
        while(i >= 0 && j >= 0){
            if(a[i] < b[j]) {
                a[count--] = b[j--];
            }else{
                a[count--] = a[i--];
            }
        }
        while(j>=0){
            a[count--] = b[j--];
        }
        return a;
    }

}
